#!/usr/bin/env python

# Author                 Version      Up-Date
# -----------------------------------------------
# L. Fraga (SOAR)         1.0        29 Sep 2009

import sys
from pyraf import iraf
from iraf import pysoar
from iraf import soi
from mosaic import mosaic
from pyraf.irafglobals import yes,no
import pysoarlib as lib
import soarprint

def soireduce(images,   \
               output,   \
               logfile,  \
               merge,    \
               verbose,  \
               xtalkcor, \
               fixpix,   \
               overscan, \
               trim,     \
               zerocor,  \
               darkcor,  \
               flatcor,  \
               sflatcor, \
               fixfile,  \
               biassec,  \
               trimsec,  \
               zero,     \
               dark,     \
               flat,     \
               sflat,    \
               minreplace):

        iraf.mscred(_doprint=0)

        infiles  = iraf.files(images,Stdout=1)
        outfiles = iraf.files(output,Stdout=1)
        
        lib.comparelists(infiles,outfiles,'infiles','outfiles')
        
        for i in range(len(infiles)):
                message = 'MESSAGE -- SOIREDUCE: WORKING ON IMAGE %s'%(infiles[i])
                soarprint.log(logfile,message,verbose)           
               
                tmpfile = iraf.mktemp('tmp') + '.fits'
                
	        iraf.ccdproc.setParam('verbose',verbose)
	        iraf.ccdproc.setParam('xtalkcor',xtalkcor)
	        iraf.ccdproc.setParam('fixpix',fixpix)
	        iraf.ccdproc.setParam('overscan',overscan)
	        iraf.ccdproc.setParam('trim',trim)
	        iraf.ccdproc.setParam('zerocor',zerocor)
	        iraf.ccdproc.setParam('darkcor',darkcor)
	        iraf.ccdproc.setParam('flatcor',flatcor)
	        iraf.ccdproc.setParam('sflatcor',sflatcor)
	        iraf.ccdproc.setParam('fixfile',fixfile)
	        iraf.ccdproc.setParam('biassec',biassec)
	        iraf.ccdproc.setParam('trimsec',trimsec)
	        iraf.ccdproc.setParam('zero',zero)
	        iraf.ccdproc.setParam('dark',dark)
	        iraf.ccdproc.setParam('flat',flat)
	        iraf.ccdproc.setParam('sflat',sflat)
	        iraf.ccdproc.setParam('minreplace',minreplace)
                iraf.ccdproc.setParam('images',infiles[i])
                iraf.ccdproc.setParam('output',tmpfile)
                iraf.ccdproc(mode='h',Stdout=1)
                    
                if merge:
                    try:
                            message = 'MESSAGE -- SOIREDUCE: MERGING IMAGE %s'%(infiles[i])
                            soarprint.log(logfile,message,verbose)           
                            mosaic(tmpfile,outfiles[i])
                            #TODO: include badpixel mask here. mask+binning.pl on pysoar$soi/data/masks.
                            #iraf.fixpix(outfiles[i],masks=maskfile)
                    except:
                            message = 'ERROR -- SOIREDUCE.MOSAIC: IN FILE %s'%(infiles[i])
                            soarprint.err(logfile,message)           
                else:
                    iraf.imcopy(tmpfile,outfiles[i])
                
                message = 'MESSAGE -- SOIREDUCE: FILE %s HAS BEEN CREATED'%(outfiles[i])
                soarprint.log(logfile,message,verbose)           
                iraf.imdelete(images=tmpfile,verify='no',default_acti ='yes')
        message = 'MESSAGE -- SOIREDUCE HAS ENDED.'
        soarprint.log(logfile,message,verbose)           

parfile = iraf.osfn("pysoar$soi/soireduce.par")
t = iraf.IrafTaskFactory(taskname="soireduce",value=parfile,function=soireduce)
